using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using Sedna.Core;
using Sedna.Core.AppConfig;
using Sedna.Core.Event;

namespace Sedna.Core.Event
{
    public class EmailEventSender: IEventSender
    {
        #region Properties
        
        private string From
        {
            get { return AppConfigurationHolder.AllerEmailFrom(); }
        }

        private string To
        {
            get { return AppConfigurationHolder.AllerEmailFrom(); }
        }

        #endregion
        
        public void Send(IEvent eventObject)
        {
            BaseEvent baseEvent = (BaseEvent) eventObject;

            MailMessage message = new MailMessage(From, To);
            message.Subject = GetMessageSubject(eventObject);
            message.Body = BuildMessageBody(baseEvent == null ? null : baseEvent).ToString();

//            SmtpClient client = new SmtpClient(MailServer);
            SmtpClient client = new SmtpClient();
            // Add credentials if the SMTP server requires them.
            //client.Credentials = CredentialCache.DefaultNetworkCredentials;
            client.Send(message);            
        }

        private StringBuilder BuildMessageBody(Object eventSource)
        {
            StringBuilder result = null;
            if (eventSource != null)
                result = DoBuildMessageBody(eventSource);
            
            if (result == null)
                result = new StringBuilder();
            
            return result;
        }
        
        protected virtual StringBuilder DoBuildMessageBody(Object eventSource)
        {
            StringBuilder result = new StringBuilder();
            return result;
        }

        protected virtual String GetMessageSubject(Object eventSource)
        {
            return "Business Event Raised";
        }

    }
}
